//
//  YTNumInputManager.m
//  KoalaPad
//
//  Created by 张英堂 on 15/4/15.
//  Copyright (c) 2015年 zyt. All rights reserved.
//


#define KTAGSTART 100

#import "YTNumInputManager.h"
#import "UIView+Marker.h"
#import "UIView+DrawLine.h"

@interface YTNumInputManager ()
{
    NSInteger _numCount;
}
@property (nonatomic, copy) NSString *showString;

@end

@implementation YTNumInputManager


- (instancetype)init{
    self = [super init];
    if (self) {
        
        _numPad = [[YTNumPad alloc] init];
        [_numPad setDelegate:self];
        
        _showTextView = [[UIView alloc] init];
        
        self.showString = @"";
        
        _numCount = 0;
        
    }
    return self;
}


- (void)show{
    if ([self checkSet] == NO) {
        return;
    }
    for (UIView *view in _showTextView.subviews) {
        [view removeFromSuperview];
    }
    CGFloat width = _showTextView.frame.size.width / self.inputCount;
    CGFloat heiht = _showTextView.frame.size.height;
    
    for (int i = 0; i < self.inputCount; i++) {
        CGRect rect = CGRectMake(width*i, 0, width, heiht);
        
        UILabel *label = [[UILabel alloc] initWithFrame:rect];
        [_showTextView addSubview:label];
        [label setTextAlignment:NSTextAlignmentCenter];
        [label setFont:[UIFont systemFontOfSize:heiht/8*5]];
        [label drawLineWidth:3.f color:[UIColor whiteColor] startPoint:CGPointMake(width*0.25, heiht-3) endPoint:CGPointMake(width*0.75, heiht-2)];
        [label setTextColor:[UIColor whiteColor]];
        
        label.tag = KTAGSTART + i;
    }
}

- (void)deleteAllText{

    self.showString = @"";
    _numCount = 0;
    
    for (UILabel *label in _showTextView.subviews) {
        [label setText:@""];
    }
}

- (void)startMark{
    if ([self checkSet] == NO) {

        return;
    }
    
    [self setMakerLocation:_numCount];
}

- (void)setMakerLocation:(NSInteger)location{
    
    if (location >= self.inputCount) {
        [_showTextView stopMarker];
        return;
    }
    
    [_showTextView starMarker];

    if (location < 0) {
        [_showTextView setMarkerOffsetX:_showTextView.frame.size.width / self.inputCount * 0.5];
        
        return;
    }
    
    [_showTextView setMarkerOffsetX:_showTextView.frame.size.width / self.inputCount * location + _showTextView.frame.size.width / self.inputCount / 2];
}

- (BOOL)checkSet{
    if (_showTextView.frame.size.width == 0 || _showTextView.frame.size.height == 0 ) {
        NSLog(@"error: 未设置显示view 的 frame");

        return NO;
    }
    if (self.inputCount <= 0) {
        NSLog(@"error: 请输入显示个数");
        return NO;
    }
    return YES;
}

#pragma mark -
- (void)numPad:(YTNumPad *)numpad clickNum:(NSInteger)num{
    
    if (_numCount < self.inputCount) {
        
        UILabel *label = (UILabel *)[_showTextView viewWithTag:KTAGSTART + _numCount];
        [label setText:[NSString stringWithFormat:@"%zi", num]];
        
        self.showString = [NSString stringWithFormat:@"%@%zi", self.showString, num];
        
        _numCount ++;
        
        [self setMakerLocation:_numCount];
    }
}

-(void)numPadClickDelete:(YTNumPad *)numpad{
    
    if (_numCount > 0) {
        
        UILabel *label = (UILabel *)[_showTextView viewWithTag:KTAGSTART + _numCount - 1];
        [label setText:[NSString stringWithFormat:@""]];
        
        if (self.showString.length >= 1) {
            self.showString = [self.showString substringToIndex:self.showString.length - 1];
        }
        
        _numCount--;
        
        [self setMakerLocation:_numCount];
    }
}

-(void)numPadClickReturn:(YTNumPad *)numpad{
    if (_numCount == self.inputCount) {
        
        if (self.returnBlock) {
            self.returnBlock(self.showString);
        }
        
    }
}

/*
 if (userPhone.length == 4) {
 
 NSMutableArray *array = [[UserManager shareManager] findUserWithPhone:userPhone];
 if (array.count >= 2) {
 
 MoreViewController *moreVC = [[MoreViewController alloc] initWithNibName:@"MoreViewController" bundle:nil];
 moreVC.dataArray = array;
 
 [self.navigationController pushViewController:moreVC animated:YES];
 
 [moreVC setFinish:^(id XXXX){
 self.selectedModel = XXXX;
 
 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
 
 [self showUserInfoView:self.selectedModel];
 });
 }];
 
 }else if (array.count == 0){
 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"查无此人" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
 [alertView show];
 }else{
 self.selectedModel = array[0];
 
 [self showUserInfoView:self.selectedModel];
 }
 
 }else{
 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入手机号码后四位" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
 [alertView show];
 }*/

@end
